---
title: "Replication extension"
author: "Muqi Guo & Jessica Van Meir"
date: "11/27/2021"
output: pdf_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = TRUE)
```

```{r}
rm(list=ls())
library(plyr)
library('ggmap')
library("mapproj")
library('coefplot')
library('gmapsdistance')
library("googleway")
library('plotrix')
library('questionr')
library('lattice')
library('stargazer')
library('aods3')
library('lmtest')
library('matrixStats')
library('tidyr')
library('readr')
library("cowplot")
library("mediation")
library("dplyr")
library("gt")

set.seed(8675309)


library(sandwich)
library(Matrix)
```

```{r}
#Muqi's data path
setwd("G:/My Drive/GOV 2001 Replication Paper/India lottery paper/dataverse_files")

#Jessica's data path
#setwd('~/Dropbox/school/Harvard/2021-2022/Gov 2001 - Quant Methods/Replication Paper/dataverse_files')
```

Harvard DataVerse access: https://doi.org/10.7910/DVN/NKENQE. Or search "Replication Data for: Home-price subsidies increase local-level political participation in urban India"

Background
In the original paper, the author investigated whether a home-price subsidies program improved individual’s political participation or not in Mumbai, an urban area in India. 
The home-price subsidies program refers to a policy that provides households with a government-constructed home at a highly subsidized price. Households can purchase a home using the transfers and rent out the home later to get additional income from rental prices. This policy in Mumbai was implemented through lotteries. Residents can enter the pool of lotteries if they are economically weaker section or low-income group urban residents who 1) do not own housing, 2) who have lived in the state of Maharashtra for at least 15 continuous years within the 20 years prior to the sale. The local government implemented the lottery using a protected computerized process since 2010, which makes the lottery fair or truly randomized. 
Data generation process
For the 2012 and 2014 lotteries, there were more than 300,000 eligible applicants for roughly 900 spots. There are two lotteries. However, no household was drawn more than once. The local authority provided phone numbers and addresses for winners and a random sample of applicants. The author interviewed a random sample of applicants through in-person household surveys of both winning (treatment) and non-winning (control) households. The process of sampling applicants for household surveys was the same as the stratified sampling method used for selecting winners. Finally, the author got a total of 1862 pre-treatment addresses, with an equal number of treated and control units in each stratum. 
The author located the addresses of these households on Google Maps. After removing 42 incomplete addresses, 611 outside of Greater Mumbai addresses, and 146 addresses that could not be mapped, 531 treatment and 532 control households left. Then the author randomly selected 500 of the mapped households from each treatment condition to interview. The author conducted the household surveys from September 2017 to May 2018, about three to five years after the lotteries. 
Applicants in the households were preferred to others to complete the household surveys. If not available, the family’s primary earner would fill in the survey. As a result, 78% of respondents had filled out the application themselves. 
The analysis sample was comprised o 834 respondents, with 413 households in the control group and 421 households in the treated group. Reasons for attrition were listed in Table SI.4, which were not different between control and treated units. Table 3 in the original paper showed the distribution of household characteristics were balanced between control and treatment groups. 
Outcomes 
The author measured political participations as claim-making behaviors. There are three types of claim-making: 1) making claims individually (corresponding question in the survey: “how often do you individually petition government officials and political leaders for something benefitting the community in which you live”; the author coded responses like “Often” or “Sometimes” as 1, “Rarely” or “Never” as 0); 2) making claims in a group (corresponding question in the survey: “how often do you participate in group petitions of government officials and political leaders for something benefitting the community in which you live”; the author coded responses like “Often” or “Sometimes” as 1, “Rarely” or “Never” as 0); 3) attending local area meeting (corresponding question in the survey: “In the last month, has anybody in the household attended a local area meeting in the neighborhood in which you live”; the author coded responses like “Yes” as 1, “No” or “Don’t know” as 0).
Potential mechanisms
The author proposed home-subsidies increased claim-making through multiple channels. First, welfare transfers can make beneficiaries wealthier, thereby increasing their time horizons and increasing their capacity for action. Second, transfers might also improve beneficiaries’ perceptions of the efficacy of their actions through improved self-perceived status and repeated interactions with government. Third, transfer can reshape citizens’ expectations of what governments can and will provide to them. Finally, transfers might change the content of claim-making. 
Statistical methods
The author evaluated the treatment effect on claim-making outcomes using an intent-to-treat (ITT) analysis as 8% of treated units did not purchase homes. The author used Benjamini-Hochberg corrections for the false discovery rate within “families” of outcomes. 
Extended analysis
The author generally discussed potential mechanisms that may link the treatment effect to claim-making outcomes. We will investigate the impact of potential mechanisms quantitatively by conducting mediation analysis. 
According the author’s discussion of mechanisms, we will focus on four attitude mediators: 1) would never consider leaving Mumbai; 2) Status relative to local leaders (corresponding question: “Do you/people like you need to listen to what leaders in the area say”, 1 if answering “No”, 0 if answering “Yes” or “Don’t know”); 3) Happy with financial situation; 4) children will have better lives than respondents themselves. 
We hypothesized that the treatment would improve attitude outcomes and then in turn improved claim-making outcomes. We will investigate the mediation effect of each mediator separately. 
There are four identification assumptions of a mediation analysis. 1) no unmeasured exposure-outcome confounders; 2) no unmeasured mediator-outcome confounders; 3) no unmeasured exposure-mediator confounders; 4) no mediator-outcome confounder affected by exposure. 
This analysis is based on a randomized process of lotteries. The four assumptions look reasonable in this analysis. 



##Table 3##
```{r}
#load in main survey data
data=read_csv('jop_data.csv')
#create centered block dummies to be used in regressions 
center_apply <- function(x) {
  apply(x, 2, function(y) y - mean(y))
}
centeredB=model.matrix( ~ as.factor(data$block) - 1) %>% center_apply(.)

#blk <- model.matrix( ~ as.factor(data$block) - 1)
#create functions for balance tables
bal_=function(x){
  
  data$x=with(data,eval(parse(text=x)))
  c= data$x[data$t==0]
  lm=lm(x~t+t*centeredB,data=data)
  vcov=vcov(lm,type='HC2')
  coef=coeftest(lm, vcov)
  bal.=as.vector(c(coef[1,1],coef[2,1],coef[2,2],coef[2,4]))%>% round(.,digits=3)
  return(bal.)
}

mean.data_=function(x){
  covs=as.data.frame(x)
  mc=(apply(covs,1,FUN=bal_))%>%t()%>%as.data.frame()
  mc=cbind.data.frame(covs,mc)
  colnames(mc)=c('Variable','Control ','Treatment ','se','Pr(>|t|)')
  return(mc)}

##prints table##
with(data,mean.data_(c('OBC','SCST','maratha','Muslim','kutchafloor', 'kutcharoof', 'always'
                      ,'same')))%>%stargazer(.,summary=FALSE,rownames = FALSE,type="text")
```

##Figure 1##
#The following three functions work together to create plots for each family of outcomes#
```{r}
TE=function(Y){ #main regession specification
  lm=lm(Y~t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same+t*centeredB, data=data) 
  vcov=vcov(lm, type="HC2")
  return(coeftest(lm, vcov)[2,])
}

mf=function(df,name){ #creates an object from the regression that can be used to make a graph
  data.frame(Variable = name %>% as.character(),
             Coefficient = df[1],
             SE = df[2],
             p=df[4]) %>%return()
}

graph=function(dataframe, xlab, ylab=FALSE, order=TRUE, p.adj=TRUE,py=0.30,ylim=.40 ){ #creates the graphs with multiple hypothesis adjusting
  allModelFrame=as.data.frame((dataframe))
  
  if (order==TRUE){allModelFrame=allModelFrame[order(allModelFrame$Coefficient),] 
  allModelFrame$Variable <- factor(allModelFrame$Variable, levels =allModelFrame$Variable[order(allModelFrame$Coefficient)])}
  if (p.adj==TRUE){allModelFrame$adj.p=p.adjust(allModelFrame$p,method="BH") #benjamini hochberg adjustments
  allModelFrame$adj.p=as.character(round(allModelFrame$adj.p, digits=3))
  allModelFrame$p=as.character(round(allModelFrame$p, digits=3))
  allModelFrame$p[allModelFrame$p=='0']='<.001'
  allModelFrame$adj.p[allModelFrame$adj.p=='0']='<.001'
  
  allModelFrame$ptext=allModelFrame$adj.p}
  
  if (p.adj==FALSE){
    allModelFrame$ptext="" #in case no adjusted p values are descired
  }
  # Specify the width of your confidence intervals
  interval1 <- -qnorm((1-0.9)/2)  # 90% multiplier
  interval2 <- -qnorm((1-0.95)/2)  # 95% multiplier
  
  
  # Plot
  zp1 <- ggplot(allModelFrame)
  zp1 <- zp1 + geom_hline(yintercept = 0, colour = gray(1/2), lty = 2)
  zp1 <- zp1 + geom_linerange(aes(x = Variable, ymin = Coefficient - SE*interval1,
                                  ymax = Coefficient + SE*interval1),
                              lwd = 1, position = position_dodge(width = 1/2))
  zp1 <- zp1 + geom_pointrange(aes(x = Variable, y = Coefficient, ymin = Coefficient - SE*interval2,
                                   ymax = Coefficient + SE*interval2),
                               lwd = 1/2, position = position_dodge(width = 1/2),
                               shape = 20)
  zp1 <- zp1 + coord_flip() + theme_bw()+geom_text(aes(x=Variable,y=py,label=as.character(ptext)),size=2.2)+
    expand_limits(y=ylim)
  zp1 <- zp1 + labs(y='Coefficient', x=xlab)+ theme(axis.text=element_text(size=8))
    if (ylab==FALSE){
      zp1=zp1+theme(axis.title.x = element_blank())}
  return(zp1)
}

#The following code creates a plot for each of the families of outcomes. 
#It then creates one larger figure in the paper at the end. 

#Claim-making
#collect individual level treatment effects
m1=TE(I(data$comp_ind=='Often'|data$comp_ind=='Sometimes')) %>% mf(.,"Make claims individually")
m2=TE(I(data$comp_group=='Often'|data$comp_group=='Sometimes')) %>% mf(.,"Make claims in a group")
m3=TE(I(data$meeting)) %>% mf(.,"Attending local area meeting")
#combine effects in plot for family 1
one=graph(rbind(m3,m2, m1),py=.6, ylim=c(-.1,.8), xlab='Claims', order=FALSE)

#Knowledge about local government

#collect individual level treatment effects
m1=TE(data$ew.party.correct) %>% mf(.,"Correct party for corporator")
m2=TE(data$ew.rep.correct) %>% mf(.,"Correct name for corporator")
m3=TE(data$ward.rep.match) %>% mf(.,"Correct name for a corporator in admin. ward")
##combine effects in plot for family 2
two=graph(rbind(m3,m2,m1), order=FALSE, xlab='Knowledge',py=.6, ylim=c(-.1,.8))

#Policy preferences
m1=TE(data$sw) %>% mf(.,"Social Welfare")
m2=TE(data$streets) %>% mf(.,"Street maintenence")
m3=TE(data$wes) %>%mf(.,"Water, electricity, sewage")
m4=TE(data$lao) %>% mf(.,"Law and order")
m5=TE(data$rsv) %>%mf(.,"Regulating street vendors")
m6=TE(data$hlu) %>% mf(.,"Housing and land use")
m7=TE(data$edu) %>% mf(.,"Education")
m8=TE(data$tran) %>% mf(.,"Transportation")
#Plot for family 3
three=graph(rbind(m1,m2,m3,m4,m5,m6,m7,m8),xlab="Policy prefs.",ylab=FALSE,py=.6, ylim=c(-.1,.8))

#Attitudes

m1=TE(I(data$finance=='Happy'))%>% mf(.,"Happy w/ financial situation") 

m2=TE(I(data$children=='Yes'))%>% mf(.,'Children will have better lives than them') 

m3=TE(I(data$leave_bom=='Would never leave'))%>% mf(.,'Would never consider leaving Mumbai')

m4=TE(I(data$leaders_status=='No')) %>% mf(.,"Status relative to local leaders")

#Plot for family 4
four=graph(rbind(m4,m3, m2,m1), order=FALSE,  xlab='Attitudes',  py=.6, ylim=c(-.1,.8))

#Voting (reasons)

m1=TE(data$vparty) %>% mf(.,"Party")
m2=TE(data$veth) %>% mf(.,"Ethnicity or religion")
m3=TE(data$vneigh) %>% mf(.,"Solving problems in the neighborhood")
m4=TE(data$vfinan) %>% mf(.,"Financial considerations")
m5=TE(data$vpref) %>% mf(.,"Representing city-wide policy preferences")
m6=TE(data$vbetter) %>% mf(.,"Long-term betterment of the Mumbai")

#Plot for family 5
five=graph(rbind(m6,m5,m4,m3,m2,m1), order=TRUE, xlab='Voting (reasons)',py=.6, ylim=c(-.1,.8))

##satisfaction with services

m1=TE(I(data$elec=='Satisfied')) %>% mf(.,"Electricity")
m2=TE(I(data$garb=='Satisfied')) %>% mf(.,"Garbage")
m3=TE(I(data$san=='Satisfied')) %>%mf(.,"Sanitation")
m4=TE(I(data$water=='Satisfied')) %>% mf(.,"Water")
m5=TE(I(data$law=='Satisfied')) %>%mf(.,"Law and order")
m6=TE(I(data$road=='Satisfied')) %>% mf(.,"Roads")
#Plot for family 6
six=graph(rbind(m1,m2,m3,m4,m5,m6),xlab='Satisfaction',ylab=TRUE,py=.6, ylim=c(-.1,.8))

##prints figure##
plot_grid(one, two,five, three, four, six, ncol=1, align="v")

```

##Extended analysis: Conduct mediation analysis of Attitudes on Claims-making - with bootstrapping##
Mediation analysis results interpretation: there are moderate mediation effects of "status relative to local leaders" and "would never consider leaving Mumbai". 
Respondents' attitude as never consider leaving Mumbai could explain 8% (95% CI: 2.5%, 16.6%) of total effect of treatment on one claim-making outcome - attending local area meeting. 
Respondents' attitude as saying no to local leaders could explain 4.6% (95% CI: 0.6%, 10.9%) of total effect of treatment on one claim-making outcome - attending local area meeting. 
```{r}
data_m <- data # Create a copy of the data

## Test "Would never consider leaving Mumbai" as a mediator on claims
# Test "Would never consider leaving Mumbai" as a mediator on "Make claims individually"
data_m <- data_m %>% mutate(stay_Mumbai = as.numeric(data$leave_bom=='Would never leave'))
model.m <- lm(stay_Mumbai ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_ind=='Often'|data_m$comp_ind=='Sometimes') ~ stay_Mumbai*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "stay_Mumbai", boot = TRUE)
cat('Test of "Would never consider leaving Mumbai" as a mediator on "Make claims individually"')
summary(m.out)
a1c1 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a1c1pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Would never consider leaving Mumbai" as a mediator on "Make claims in a group"
model.m <- lm(stay_Mumbai ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_group=='Often'|data_m$comp_group=='Sometimes') ~ stay_Mumbai*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "stay_Mumbai", boot = TRUE)
cat('Test "Would never consider leaving Mumbai" as a mediator on "Make claims in a group"')
summary(m.out)
a1c2 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a1c2pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Would never consider leaving Mumbai as a mediator on "Attending local area meeting"
model.m <- lm(stay_Mumbai ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(meeting ~ stay_Mumbai*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "stay_Mumbai", boot = TRUE)
cat('Test "Would never consider leaving Mumbai as a mediator on "Attending local area meeting"')
summary(m.out)
a1c3 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a1c3pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

## Test "Status relative to local leaders" as a mediator on claims
# Test "Status relative to local leaders" as a mediator on "Make claims individually"
data_m <- data_m %>% mutate(status_no = as.numeric(data$leaders_status=='No'))
model.m <- lm(status_no ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_ind=='Often'|data_m$comp_ind=='Sometimes') ~ status_no*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "status_no", boot = TRUE)
cat('# Test "Status relative to local leaders" as a mediator on "Make claims individually"')
summary(m.out)
a2c1 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a2c1pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Status relative to local leaders" as a mediator on "Make claims in a group"
model.m <- lm(status_no ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_group=='Often'|data_m$comp_group=='Sometimes') ~ status_no*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "status_no", boot = TRUE)
cat('Test "Status relative to local leaders" as a mediator on "Make claims in a group"')
summary(m.out)
a2c2 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a2c2pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Status relative to local leaders" as a mediator on "Attending local area meeting"
model.m <- lm(status_no ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(meeting ~ status_no*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "status_no", boot = TRUE)
cat('Test "Status relative to local leaders" as a mediator on "Attending local area meeting"')
summary(m.out)
a2c3 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a2c3pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

## Test "Happy with financial situation" as a mediator on claims
# Test "Happy with financial situation" as a mediator on "Make claims individually"
data_m <- data_m %>% mutate(finance_happy = as.numeric(data$finance=='Happy'))
model.m <- lm(finance_happy ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_ind=='Often'|data_m$comp_ind=='Sometimes') ~ finance_happy*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "finance_happy", boot = TRUE)
cat('Test "Happy with financial situation" as a mediator on "Make claims individually"')
summary(m.out)
a3c1 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a3c1pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Happy with financial situation" as a mediator on "Make claims in a group"
model.m <- lm(finance_happy ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_group=='Often'|data_m$comp_group=='Sometimes') ~ finance_happy*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "finance_happy", boot = TRUE)
cat('Test "Happy with financial situation" as a mediator on "Make claims in a group"')
summary(m.out)
a3c2 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a3c2pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Happy with financial situation" as a mediator on "Attending local area meeting"
model.m <- lm(finance_happy ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(meeting ~ finance_happy*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "finance_happy", boot = TRUE)
cat('Test "Happy with financial situation" as a mediator on "Attending local area meeting"')
summary(m.out)
a3c3 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a3c3pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

## Test "Children will have better lives than them" as a mediator on claims
# Test "Children will have better lives than them" as a mediator on "Make claims individually"
data_m <- data_m %>% mutate(children_better = as.numeric(data$children=='Yes'))
model.m <- lm(children_better ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_ind=='Often'|data_m$comp_ind=='Sometimes') ~ children_better*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "children_better", boot = TRUE)
cat('Test "Children will have better lives than them" as a mediator on "Make claims individually"')
summary(m.out)
a4c1 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a4c1pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Children will have better lives than them" as a mediator on "Make claims in a group"
model.m <- lm(children_better ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(I(data_m$comp_group=='Often'|data_m$comp_group=='Sometimes') ~ children_better*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "children_better", boot = TRUE)
cat('Test "Children will have better lives than them" as a mediator on "Make claims in a group"')
summary(m.out)
a4c2 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a4c2pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

# Test "Children will have better lives than them" as a mediator on "Attending local area meeting"
model.m <- lm(children_better ~ t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
model.y <- lm(meeting ~ children_better*t+OBC+SCST+maratha+Muslim+kutchafloor+kutcharoof+always+same, data=data_m) #removed t*centeredB
m.out <- mediate(model.m, model.y, treat = "t", mediator = "children_better", boot = TRUE)
cat('Test "Children will have better lives than them" as a mediator on "Attending local area meeting"')
summary(m.out)
a4c3 <- tibble("ACME_treated" = m.out$d1, "ACME_treated_p" = m.out$d1.p,
               "ADE_control"= m.out$z0, "ADE_control_p" = m.out$z0.p,
               "PropMediated_treated" = m.out$n1, "PropMediated_treated_p" = m.out$n1.p)
a4c3pt <- tibble("Estimate" = m.out$n1, "Lower bound" = m.out$n1.ci[1], "Upper bound" = m.out$n1.ci[2], "p-value" = m.out$n1.p)

## Present results of mediation tests of all attitudes on all claims
attitudes_claims <- rbind(a1c1, a1c2, a1c3, a2c1, a2c2, a2c3, a3c1, a3c2, a3c3, a4c1, a4c2, a4c3)
attitudes_claims <- attitudes_claims %>% 
  mutate(attitudes = c("Would never consider leaving Mumbai", "Would never consider leaving Mumbai", "Would never consider leaving Mumbai", "Status relative to local leaders", "Status relative to local leaders", "Status relative to local leaders", "Happy w/ financial situation", "Happy w/ financial situation", "Happy w/ financial situation", "Children will have better lives than them", "Children will have better lives than them", "Children will have better lives than them"), 
          claims = c("Individual", "Group", "Meeting", "Individual", "Group", "Meeting", "Individual", "Group", "Meeting", "Individual", "Group", "Meeting"), .before = 1)
gt(attitudes_claims)

attitudes_claims_propmed_treated <- rbind(a1c1pt, a1c2pt, a1c3pt, a2c1pt, a2c2pt, a2c3pt, a3c1pt, a3c2pt, a3c3pt, a4c1pt, a4c2pt, a4c3pt)
attitudes_claims_propmed_treated <- attitudes_claims_propmed_treated %>% 
  mutate(attitudes = c("Would never consider leaving Mumbai", "Would never consider leaving Mumbai", "Would never consider leaving Mumbai", "Status relative to local leaders", "Status relative to local leaders", "Status relative to local leaders", "Happy w/ financial situation", "Happy w/ financial situation", "Happy w/ financial situation", "Children will have better lives than them", "Children will have better lives than them", "Children will have better lives than them"), 
          claims = c("Individual", "Group", "Meeting", "Individual", "Group", "Meeting", "Individual", "Group", "Meeting", "Individual", "Group", "Meeting"), .before = 1)
gt(attitudes_claims_propmed_treated) %>%
  tab_header(title = "Proportion mediated (treated) for attitutes as a mediator on claims")

```

